package com.tool.cn.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

/**
 * 字节数组工具类
 * 
 * @author wangzh 2017年12月18日 上午11:29:01
 */
public class ByteUtil {

	/**
	 * long 转 byte
	 */
	public static byte[] longToBytes(long n) {
		byte[] b = new byte[8];
		b[7] = (byte) (n & 0xff);
		b[6] = (byte) (n >> 8 & 0xff);
		b[5] = (byte) (n >> 16 & 0xff);
		b[4] = (byte) (n >> 24 & 0xff);
		b[3] = (byte) (n >> 32 & 0xff);
		b[2] = (byte) (n >> 40 & 0xff);
		b[1] = (byte) (n >> 48 & 0xff);
		b[0] = (byte) (n >> 56 & 0xff);
		return b;
	}

	/**
	 * long 追加到byte数组
	 * 
	 * @param n
	 *            添加值
	 * @param array
	 *            数组
	 * @param offset
	 *            偏移量
	 */
	public static void longToBytes(long n, byte[] array, int offset) {
		array[7 + offset] = (byte) (n & 0xff);
		array[6 + offset] = (byte) (n >> 8 & 0xff);
		array[5 + offset] = (byte) (n >> 16 & 0xff);
		array[4 + offset] = (byte) (n >> 24 & 0xff);
		array[3 + offset] = (byte) (n >> 32 & 0xff);
		array[2 + offset] = (byte) (n >> 40 & 0xff);
		array[1 + offset] = (byte) (n >> 48 & 0xff);
		array[0 + offset] = (byte) (n >> 56 & 0xff);
	}

	/**
	 * bytes 转 long
	 */
	public static long bytesToLong(byte[] array) {
		return ((((long) array[0] & 0xff) << 56) | (((long) array[1] & 0xff) << 48) | (((long) array[2] & 0xff) << 40)
				| (((long) array[3] & 0xff) << 32) | (((long) array[4] & 0xff) << 24) | (((long) array[5] & 0xff) << 16)
				| (((long) array[6] & 0xff) << 8) | (((long) array[7] & 0xff) << 0));
	}

	/**
	 * byte数组 转 long offset 偏移量
	 */
	public static long bytesToLong(byte[] array, int offset) {
		return ((((long) array[offset + 0] & 0xff) << 56) | (((long) array[offset + 1] & 0xff) << 48)
				| (((long) array[offset + 2] & 0xff) << 40) | (((long) array[offset + 3] & 0xff) << 32)
				| (((long) array[offset + 4] & 0xff) << 24) | (((long) array[offset + 5] & 0xff) << 16)
				| (((long) array[offset + 6] & 0xff) << 8) | (((long) array[offset + 7] & 0xff) << 0));
	}

	/**
	 * int 转 byte
	 */
	public static byte[] intToBytes(int n) {
		byte[] b = new byte[4];
		b[3] = (byte) (n & 0xff);
		b[2] = (byte) (n >> 8 & 0xff);
		b[1] = (byte) (n >> 16 & 0xff);
		b[0] = (byte) (n >> 24 & 0xff);
		return b;
	}

	/**
	 * int 追加byte数组
	 *
	 */
	public static void intToBytes(int n, byte[] array, int offset) {
		array[3 + offset] = (byte) (n & 0xff);
		array[2 + offset] = (byte) (n >> 8 & 0xff);
		array[1 + offset] = (byte) (n >> 16 & 0xff);
		array[offset] = (byte) (n >> 24 & 0xff);
	}

	/**
	 * byte 转 int
	 */
	public static int bytesToInt(byte b[]) {
		return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24;
	}

	/**
	 * byte 转 int
	 */
	public static int bytesToInt(byte b[], int offset) {
		return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8 | (b[offset + 1] & 0xff) << 16
				| (b[offset] & 0xff) << 24;
	}

	/**
	 * unit 转 hyte 数组
	 * 
	 */
	public static byte[] uintToBytes(long n) {
		byte[] b = new byte[4];
		b[3] = (byte) (n & 0xff);
		b[2] = (byte) (n >> 8 & 0xff);
		b[1] = (byte) (n >> 16 & 0xff);
		b[0] = (byte) (n >> 24 & 0xff);

		return b;
	}

	/**
	 * byte数组追加unit
	 */
	public static void uintToBytes(long n, byte[] array, int offset) {
		array[3 + offset] = (byte) (n);
		array[2 + offset] = (byte) (n >> 8 & 0xff);
		array[1 + offset] = (byte) (n >> 16 & 0xff);
		array[offset] = (byte) (n >> 24 & 0xff);
	}

	/**
	 * 数组 转 unit
	 */
	public static long bytesToUint(byte[] array) {
		return ((long) (array[3] & 0xff)) | ((long) (array[2] & 0xff)) << 8 | ((long) (array[1] & 0xff)) << 16
				| ((long) (array[0] & 0xff)) << 24;
	}

	/**
	 * 数据转unit
	 */
	public static long bytesToUint(byte[] array, int offset) {
		return ((long) (array[offset + 3] & 0xff)) | ((long) (array[offset + 2] & 0xff)) << 8
				| ((long) (array[offset + 1] & 0xff)) << 16 | ((long) (array[offset] & 0xff)) << 24;
	}

	/**
	 * short 转 byte数组
	 */
	public static byte[] shortToBytes(short n) {
		byte[] b = new byte[2];
		b[1] = (byte) (n & 0xff);
		b[0] = (byte) ((n >> 8) & 0xff);
		return b;
	}

	/**
	 * byte数组追加short
	 */
	public static void shortToBytes(short n, byte[] array, int offset) {
		array[offset + 1] = (byte) (n & 0xff);
		array[offset] = (byte) ((n >> 8) & 0xff);
	}

	/**
	 * byte数组 转 short
	 */
	public static short bytesToShort(byte[] b) {
		return (short) (b[1] & 0xff | (b[0] & 0xff) << 8);
	}

	/**
	 * byte数组 转short
	 */
	public static short bytesToShort(byte[] b, int offset) {
		return (short) (b[offset + 1] & 0xff | (b[offset] & 0xff) << 8);
	}

	/**
	 * 无符号short转byte数组
	 */
	public static byte[] ushortToBytes(int n) {
		byte[] b = new byte[2];
		b[1] = (byte) (n & 0xff);
		b[0] = (byte) ((n >> 8) & 0xff);
		return b;
	}

	/**
	 * 无符号 short转byte数组
	 */
	public static void ushortToBytes(int n, byte[] array, int offset) {
		array[offset + 1] = (byte) (n & 0xff);
		array[offset] = (byte) ((n >> 8) & 0xff);
	}

	/**
	 * byte数组转无符号short
	 */
	public static int bytesToUshort(byte b[]) {
		return b[1] & 0xff | (b[0] & 0xff) << 8;
	}

	/**
	 * byte数组转无符号short
	 */
	public static int bytesToUshort(byte b[], int offset) {
		return b[offset + 1] & 0xff | (b[offset] & 0xff) << 8;
	}

	/**
	 * 无符号bytes数组转 有符号数组
	 */
	public static byte[] ubyteToBytes(int n) {
		byte[] b = new byte[1];
		b[0] = (byte) (n & 0xff);
		return b;
	}

	/**
	 * 无符号bytes数组转 有符号数组
	 */
	public static void ubyteToBytes(int n, byte[] array, int offset) {
		array[0] = (byte) (n & 0xff);
	}

	/**
	 * byte数组转无符号数组
	 */
	public static int bytesToUbyte(byte[] array) {
		return array[0] & 0xff;
	}

	/**
	 * byte数组转无符号数组
	 */
	public static int bytesToUbyte(byte[] array, int offset) {
		return array[offset] & 0xff;
	}

	/**
	 * 字节数组反转
	 * 
	 * @param src
	 * @return
	 */
	public static byte[] arrReverse(byte[] src) {
		byte[] obj = new byte[src.length];
		for (int i = 0; i < src.length; i++) {
			obj[i] = src[src.length - 1 - i];
		}
		return obj;
	}

	/**
	 * 数组截取 start 起始位置 length 截取长度
	 */
	public static byte[] getArray(byte[] srcArr, int start, int length) {
		byte[] arr = new byte[length];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = srcArr[start + i];
		}
		// System.arraycopy(srcArr, start, arr, 0, length);
		return arr;
	}

	/**
	 * 数组截取 reverse 网络序 false 主机序 true
	 */
	public static byte[] getArray(byte[] srcArr, int start, int length, boolean reverse) {
		byte[] arr = new byte[length];
		if (reverse) {
			int start2 = start + arr.length - 1;
			for (int i = 0; i < arr.length; i++) {
				arr[i] = srcArr[start2 - i];
			}
		} else {
			for (int i = 0; i < arr.length; i++) {
				arr[i] = srcArr[start + i];
			}
		}
		return arr;
	}

	/**
	 * byte数组追加 destStart 开始下标 length 追加数组长度
	 */
	public static void arraycopy(byte[] srcArr, int srcstart, byte[] destArr, int destStart, int length) {
		if (srcArr == null || length + srcstart > srcArr.length || destArr == null
				|| length + destStart > destArr.length)
			return;

		for (int i = 0; i < length; i++) {
			destArr[destStart + i] = srcArr[srcstart + i];
		}

	}

	/**
	 * byte数组追加
	 */
	public static byte[] appendArray(byte[] a, byte[] b) {
		byte[] c = new byte[a.length + b.length];
		for (int i = 0; i < a.length; i++) {
			c[i] = a[i];
		}
		for (int j = 0; j < b.length; j++) {
			c[a.length + j] = b[j];
		}
		return c;
	}

	/**
	 * 指定长度转化bigInteger，针对0000XXXX类型的数据
	 * 
	 * @return
	 */
	public static byte[] getArrayByBigInt(BigInteger b, int len) {
		byte[] tmp = b.toByteArray();
		byte[] result = new byte[len];
		if (tmp.length <= len) {
			System.arraycopy(tmp, 0, result, len - tmp.length, tmp.length);
			for (int i = 0; i < (result.length - tmp.length); i++) {
				result[i] = 0;
			}
		} else {
			result = getArray(tmp, tmp.length - len, len);
		}
		return result;
	}

	/**
	 * 根据字节数组将long转换高低位
	 */
	public long toLong(byte[] old) {
		long ds[] = new long[8];

		ds[7] = (long) (old[7]);
		ds[6] = (long) (old[6]);
		ds[5] = (long) (old[5]);
		ds[4] = (long) (old[4]);
		ds[3] = (long) (old[3]);
		ds[2] = (long) (old[2]);
		ds[1] = (long) (old[1]);
		ds[0] = (long) (old[0]);

		return (((ds[7] & 0xff) << 56) + ((ds[6] & 0xff) << 48) + ((ds[5] & 0xff) << 40) + ((ds[4] & 0xff) << 32)
				+ ((ds[3] & 0xff) << 24) + ((ds[2] & 0xff) << 16) + ((ds[1] & 0xff) << 8) + ((ds[0] & 0xff) << 0));
	}

	/**
	 * 字节数组转16进制字符串
	 */
	public static String getHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	/**
	 * hex （16进制转）byte[]数组
	 */
	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	/**
	 * char 转byte
	 */
	private static byte charToByte(char c) {
		if (c >= 48 && c <= 57)
			return (byte) (c - 48);
		else if (c >= 65 && c <= 70)
			return (byte) (c - 55);
		if (c >= 97 && c <= 102)
			return (byte) (c - 87);
		else
			return 0;
	}
	
	/**
	 * 输入流转byte数组
	 * @param stream
	 * @return
	 * @throws IOException
	 */
	public static byte[] toByteArray(InputStream stream) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		byte[] buffer = new byte['?'];
		int read = 0;
		while (read != -1) {
			read = stream.read(buffer);
			if (read > 0) {
				baos.write(buffer, 0, read);
			}
		}

		return baos.toByteArray();
	}

}
